package cn.iocoder.yudao.module.lfd.controller.admin.shop;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.annotation.security.PermitAll;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.lfd.controller.admin.shop.vo.*;
import cn.iocoder.yudao.module.lfd.dal.dataobject.shop.LfdShopDO;
import cn.iocoder.yudao.module.lfd.convert.shop.ShopConvert;
import cn.iocoder.yudao.module.lfd.service.shop.ShopService;

@Tag(name = "管理后台 - 店铺")
@RestController
@RequestMapping("/lfd/shop")
@Validated
public class ShopController {

    @Resource
    private ShopService shopService;

    @PostMapping("/create")
    @Operation(summary = "创建店铺")
    @PreAuthorize("@ss.hasPermission('lfd:shop:create')")
    public CommonResult<Long> createShop(@Valid @RequestBody ShopCreateReqVO createReqVO) {
        return success(shopService.createShop(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新店铺")
    @PreAuthorize("@ss.hasPermission('lfd:shop:update')")
    public CommonResult<Boolean> updateShop(@Valid @RequestBody ShopUpdateReqVO updateReqVO) {
        shopService.updateShop(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除店铺")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('lfd:shop:delete')")
    public CommonResult<Boolean> deleteShop(@RequestParam("id") Long id) {
        shopService.deleteShop(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得店铺")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('lfd:shop:query')")
    public CommonResult<ShopRespVO> getShop(@RequestParam("id") Long id) {
        LfdShopDO shop = shopService.getShop(id);
        return success(ShopConvert.INSTANCE.convert(shop));
    }

    @GetMapping("/list")
    @Operation(summary = "获得店铺列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('lfd:shop:query')")
    public CommonResult<List<ShopRespVO>> getShopList(@RequestParam("ids") Collection<Long> ids) {
        List<LfdShopDO> list = shopService.getShopList(ids);
        return success(ShopConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得店铺分页")
    @PreAuthorize("@ss.hasPermission('lfd:shop:query')")
    public CommonResult<PageResult<ShopRespVO>> getShopPage(@Valid ShopPageReqVO pageVO) {
        PageResult<LfdShopDO> pageResult = shopService.getShopPage(pageVO);
        return success(ShopConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出店铺 Excel")
    @PreAuthorize("@ss.hasPermission('lfd:shop:export')")
    @OperateLog(type = EXPORT)
    public void exportShopExcel(@Valid ShopExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<LfdShopDO> list = shopService.getShopList(exportReqVO);
        // 导出 Excel
        List<ShopExcelVO> datas = ShopConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "店铺.xls", "数据", ShopExcelVO.class, datas);
    }

    @GetMapping("/mini-list")
    @Operation(summary = "小程序获得店铺列表")
    @PermitAll
    public CommonResult<List<ShopRespVO>> getMiniShopList() {
        List<LfdShopDO> list = shopService.getAll();
        return success(ShopConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/mini-get")
    @Operation(summary = "小程序获得店铺")
    @PermitAll
    public CommonResult<ShopRespVO> getMiniLoadt(@RequestParam("shopId") Long shopId) {
        LfdShopDO shop = shopService.getShop(shopId);
        return success(ShopConvert.INSTANCE.convert(shop));
    }
}
